#!/usr/bin/env python
#!coding=utf-8

import requests
import commands 


class Monitor:

    def __init__ (self,urls):
        self.req(urls)
    
    '''
      发送请求
    '''
    def req (self,urls) :
		
        for url in urls :

	    print url

	    try:
		    #打开异常
	            r = requests.get(url,timeout=4)

        	    if r.status_code != 200 :
                	print url+'访问不正常，返回码是'+str(r.status_code)
			return 0


	            if ((r.status_code ==  200) and (r.elapsed.microseconds > 1000000)) :
        	        print url+'访问不正常，返回码是'+str(r.status_code)
			return 0
	
	    #服务器无法连接
            except (IOError ,ZeroDivisionError),x:
		print commands.getoutput("lnmp restart")
		print x
	        return 0	




if __name__ == '__main__':

    urls = [
      'https://www.haijuw.com/',
      'http://www.haijufc.com/',
      'http://m.haijuw.com',
      'http://m.haijufc.com',
    ]

    print '时间：'+commands.getoutput('date +"%Y-%m-%d %H:%M:%S"')
    print '4秒超时检测：'


    obj = Monitor(urls)
